<?

// Built by Duncan Hodgson (dhodgson87@googlemail.com)
// Version 0.1

class plingsAPI{

	// FEED URLS
	var $feedURL = "http://feeds.plings.net/xml.activity.php/";
	var $singleURL = "http://feeds.plings.net/activity.php/";
	
	// CACHE SETTINGS
	var $cacheStatus;
	var $cacheHrs;
	var $cachePath = "cache/"; // this directory must be able to be written to by the server
	var $cacheFile;
	
	// STANDARD
	var $NumDays;
	var $ApiKey;
	
	// MODE INFO
	var $ApiMode;
	var $LACode;
	var $town;
	var $ward;
	var $postcode;
	var $venueID;
	var $activityID;
	var $providerID;
	
	// ADDITIONAL PARAMETERS
	var $searchDate;
	var $maxResults;
	var $pageNumber;
	var $onDay;
	var $weekDays;

	function getMyPlings( ){

		if($this->cacheStatus == "off"){
		
			$url = $this->buildPlingsURL();
			$xml = simplexml_load_file($url);
					
		}
		
		elseif($this->cacheStatus == "on"){

			if($this->checkForRenewal()){
		
				$url = $this->buildPlingsURL();
				$xml = simplexml_load_file($url);
				$this->makeCacheData($xml);
		
			}
			else{
		
				$url = $this->getCachedData();
				$xml = simplexml_load_file($url);
		
			}
		
		}
	
		$feedData = $xml;	
		return $feedData;
	
	}
	
	function buildPlingsURL( ){
	
		// DEFINE API MODE AND BUILD URL PART ONE

		if($this->ApiMode == "authority"){	
			$url  = $this->feedURL.$this->NumDays."/la/".$this->LACode."?";
		}
		
		elseif($this->ApiMode == "venue"){
			$url  = $this->feedURL.$this->NumDays."/v/".$this->venueID."?";
		}
		
		elseif($this->ApiMode == "provider"){
			$url  = $this->feedURL.$this->NumDays."/o/".$this->providerID."?";
		}
		
		elseif($this->ApiMode == "postcode"){
			$url  = $this->feedURL.$this->NumDays."/postcode/".$this->postcode."?";
		}
		
		elseif($this->ApiMode == "town"){
			$url  = $this->feedURL.$this->NumDays."/town/".$this->town."?";
		}
		
		elseif($this->ApiMode == "ward"){
			$url  = $this->feedURL.$this->NumDays."/ward/".$this->ward."?";
		}
		
		elseif($this->ApiMode == "activity"){
   			$url = $this->singleURL.$this->activityID.".xml?";
		}

	// DEFINE ADDITIONAL PARAMETERS

		if($this->searchDate){
			$url = $url."searchDate=".$this->searchDate."&";		
		}
		
		if($this->maxResults){
			$url = $url."MaxResults=".$this->maxResults."&";		
		}
		
		if($this->pageNumber){
			$url = $url."Page=".$this->pageNumber."&";		
		}
		
		if($this->onDay){
			$url = $url."onday=".$this->onDay."&";		
		}
		
		if($this->weekDays){
			$url = $url."weekdays=".$this->weekDays."&";		
		}
	
	// ADD API KEY
		
		$url = $url."APIKey=".$this->ApiKey;
		
		return $url;
	
	}
	
	function getCachedData(){
	
		$url = $this->cachePath.$this->cacheFile;
		
		return $url;
	
	}
	
	function makeCacheData($xml){
	
		file_put_contents($this->cachePath.$this->cacheFile, $xml->asXML());
	
	}
	
	function checkForRenewal(){

		$cachetime = (60 * 60 * $this->cacheHrs); 
		$filetimemod = filemtime($this->cachePath.$this->cacheFile) + $cachetime;
		if ($filetimemod < time()) {
			return true;
		} else {
			return false;
		}
	}
	
	function activityLite($activity){
	
   		$content  = "<p><b>".$activity->Name."</b>";
   		$content .= "<br>Where: ".$activity->venue->Name."<br>";
   		$day = substr($activity->Starts,8,2);
   		$mon = substr($activity->Starts,5,2);
   		$yrs = substr($activity->Starts,0,4);
   		$activity->Starts = substr($activity->Starts,11,5);
   		$activity->Ends = substr($activity->Ends,11,5);
   		$ats = mktime(0,0,0,$mon,$day,$yrs);
   		$atf = date("D d/m/y",$ats);
   		$content .= "When: ".$activity->Starts." to ".$activity->Ends." on ".$atf."<br>";
   		$content .= "Ages: ".$activity->MinAge." to ".$activity->MaxAge."<br>";
   		$content .= "Cost: &pound;".$activity->Cost."<br>";
   		$content .= $activity->Details."<br><br>";
   		$content .= "<b>For more information contact ".$activity->ContactName;
		$content .= " on ".$activity->ContactNumber."</b><br><br>";
   
   		$content .= "<i>Data on this activity was provided by ".$activity->provider->Name.".</i></p>";
		
		return $content;
	
	}

}

?>